import java.io.*;
import java.util.Vector;


public class start {

	/**
	 * @param args, start.java will start all PeerProcess on remote hosts.
	 */
	private static Vector<PeerInfo> peerInfoVector;
	private static String groupAddr;
	private static String groupPort;
	private final static long  fiveSeconds=5500;
	private static void getSystemProperties()
	{
		String st;
		peerInfoVector = new Vector<PeerInfo>();
		try {
			BufferedReader in = new BufferedReader(new FileReader("system.properties"));
			while((st = in.readLine()) != null) {
				
				 String[] tokens = st.split("=");		    
			     peerInfoVector.addElement(new PeerInfo(tokens[0], tokens[1]));			
			}			
			in.close();
			in=new BufferedReader(new FileReader("multicast.config"));
			st=in.readLine();
			String[] group=st.split(":");
			groupAddr=group[0];
			groupPort=group[1];
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		getSystemProperties();
		String path = System.getProperty("user.dir");
		for(int i=0;i<peerInfoVector.size();i++)
		{
			PeerInfo pInfo = (PeerInfo) peerInfoVector.elementAt(i);
			try {
				Runtime.getRuntime().exec("ssh " 
					+ pInfo.getAddr()+" cd "+path+
					" ; java PeerProcess "+pInfo.getPid()+
					" "+pInfo.getAddr()+
					" "+groupAddr+" "+groupPort);
				System.out.println("start "+pInfo.getPid()+" at "+pInfo.getAddr());
				try {
					Thread.sleep(fiveSeconds);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
